//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.ponly.fs.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.*;
import java.nio.charset.Charset;
import java.text.StringCharacterIterator;
import java.util.*;
import java.util.zip.CRC32;

public abstract class StringUtils {
    public static final String EMPTY_STRING = "";
    private static final String __LICENSE_DATA = "__NULL__";

    public static boolean hasLength(CharSequence charseq) {
        return charseq != null && charseq.length() > 0;
    }

    public static boolean hasText(CharSequence charseq) {
        int len;
        if(charseq != null && (len = charseq.length()) != 0) {
            for(int i = 0; i < len; ++i) {
                if(!Character.isWhitespace(charseq.charAt(i))) {
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }

    public static String slice(String str, int start, int end) {
        if(!hasLength(str)) {
            return str;
        } else {
            int len = str.length();
            start = start < 0?start + len:start;
            end = end < 0?end + len:end;
            return start > end?"":str.substring(start, end);
        }
    }

    public static String capitalize(String str) {
        if(!hasLength(str)) {
            return str;
        } else {
            char[] chars = str.toCharArray();
            chars[0] = Character.toUpperCase(chars[0]);
            return new String(chars);
        }
    }

    public static String uncapitalize(String str) {
        if(!hasLength(str)) {
            return str;
        } else if(str.length() > 1 && Character.isUpperCase(str.charAt(0)) && Character.isUpperCase(str.charAt(1))) {
            return str;
        } else {
            char[] chars = str.toCharArray();
            chars[0] = Character.toLowerCase(chars[0]);
            return new String(chars);
        }
    }

    public String swapCase(String str) {
        if(hasLength(str)) {
            return str;
        } else {
            char[] chars = str.toCharArray();

            for(int i = 0; i < chars.length; ++i) {
                char c = chars[i];
                if(Character.isLowerCase(c)) {
                    chars[i] = Character.toUpperCase(c);
                } else if(Character.isUpperCase(c)) {
                    chars[i] = Character.toLowerCase(c);
                } else if(Character.isTitleCase(c)) {
                    chars[i] = Character.toLowerCase(c);
                }
            }

            return new String(chars);
        }
    }

    public static String trim(String str) {
        return str == null?null:str.trim();
    }

    public static String ltrim(String str) {
        if(!hasLength(str)) {
            return str;
        } else {
            int st = 0;

            int len;
            for(len = str.length(); st < len && str.charAt(st) <= 32; ++st) {
                ;
            }

            return st > 0?str.substring(st, len):str;
        }
    }

    public static String rtrim(String str) {
        if(!hasLength(str)) {
            return str;
        } else {
            boolean st = false;

            int len;
            for(len = str.length(); 0 < len && str.charAt(len - 1) <= 32; --len) {
                ;
            }

            return len < str.length()?str.substring(0, len):str;
        }
    }

    public static String pad(String str, char padChar, int total, boolean lpad) {
        int len = str.length();
        if(len >= total) {
            return str;
        } else {
            StringBuilder buff = new StringBuilder();

            for(int i = 0; i < total - len; ++i) {
                buff.append(padChar);
            }

            if(lpad) {
                buff.append(str);
            } else {
                buff.insert(0, str);
            }

            return buff.toString();
        }
    }

    public static String trimToNull(String str) {
        String trim = trim(str);
        return !hasLength(trim)?null:trim;
    }

    public static String trimToEmpty(String str) {
        String trim = trim(str);
        return trim == null?"":trim;
    }

    public static String underscoreToCamelCase(String str, boolean firstCharUpperCase) {
        return delimitedToCamelCase(str, '_', firstCharUpperCase);
    }

    public static String delimitedToCamelCase(String str, char delimiter, boolean firstCharUpperCase) {
        if(!hasLength(str)) {
            return str;
        } else {
            StringCharacterIterator it = new StringCharacterIterator(str.toLowerCase(Locale.ENGLISH));
            StringBuilder segment = new StringBuilder(str.length());

            for(boolean undlerline = false; it.getIndex() < it.getEndIndex(); it.next()) {
                char c = it.current();
                if((0 != it.getIndex() || !firstCharUpperCase) && !undlerline) {
                    if(delimiter == c) {
                        undlerline = true;
                    } else {
                        segment.append(c);
                    }
                } else {
                    segment.append(Character.toUpperCase(c));
                    undlerline = false;
                }
            }

            return segment.toString();
        }
    }

    public static String camelCaseToUnderscore(String str, boolean upperCase) {
        if(!hasLength(str)) {
            return str;
        } else {
            char[] chars = str.toCharArray();
            StringBuilder segment = new StringBuilder();
            segment.append(upperCase?Character.toUpperCase(chars[0]):Character.toLowerCase(chars[0]));

            for(int i = 1; i < chars.length; ++i) {
                char c = chars[i];
                if(Character.isUpperCase(c) && i + 1 < chars.length && !Character.isUpperCase(chars[i + 1]) && 95 != chars[i + 1]) {
                    segment.append('_');
                }

                c = upperCase?Character.toUpperCase(c):Character.toLowerCase(c);
                segment.append(c);
            }

            return segment.toString();
        }
    }

    public static String clean(String in) {
        String out = in;
        if(in != null) {
            out = in.trim();
            if(out.equals("")) {
                out = null;
            }
        }

        return out;
    }

    public static String[] clean(String[] array) {
        if(array == null) {
            return new String[0];
        } else {
            ArrayList result = new ArrayList(array.length);
            String[] var2 = array;
            int var3 = array.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String s = var2[var4];
                if(null != clean(s)) {
                    result.add(s);
                }
            }

            return (String[])result.toArray(new String[result.size()]);
        }
    }



    public static boolean startsWith(String str, String prefix) {
        return startsWith(str, prefix, false);
    }

    public static boolean startsWithIgnoreCase(String str, String prefix) {
        return startsWith(str, prefix, true);
    }

    public static boolean endsWith(String str, String suffix) {
        return endsWith(str, suffix, false);
    }

    public static boolean endsWithIgnoreCase(String str, String suffix) {
        return endsWith(str, suffix, true);
    }

    protected static boolean startsWith(String str, String prefix, boolean ignoreCase) {
        return str != null && prefix != null && str.length() >= prefix.length()?(str.startsWith(prefix)?true:ignoreCase && str.substring(0, prefix.length()).toLowerCase().equals(prefix.toLowerCase())):false;
    }

    protected static boolean endsWith(String str, String suffix, boolean ignoreCase) {
        return null != str && null != suffix && str.length() >= suffix.length()?(str.endsWith(suffix)?true:ignoreCase && str.substring(str.length() - suffix.length()).toLowerCase().equals(suffix.toLowerCase())):false;
    }

    public static int indexOfIgnoreCase(String text, String search, boolean ignoreCase) {
        return indexOfIgnoreCase(text, search, 0, ignoreCase);
    }

    public static int indexOfIgnoreCase(String str, String searchStr, int startPos, boolean ignoreCase) {
        if(str != null && searchStr != null) {
            if(startPos < 0) {
                startPos = 0;
            }

            int endLimit = str.length() - searchStr.length() + 1;
            if(startPos > endLimit) {
                return -1;
            } else if(searchStr.length() == 0) {
                return startPos;
            } else {
                for(int i = startPos; i < endLimit; ++i) {
                    if(str.regionMatches(ignoreCase, i, searchStr, 0, searchStr.length())) {
                        return i;
                    }
                }

                return -1;
            }
        } else {
            return -1;
        }
    }



    public static Iterable<String> tokenize(String s) {
        return tokenize(new StringTokenizer(s));
    }

    public static Iterable<String> tokenize(String s, String separators) {
        final StringTokenizer tokenizer = new StringTokenizer(s, separators);
        return new Iterable() {
            public Iterator<String> iterator() {
                return new Iterator() {
                    public boolean hasNext() {
                        return tokenizer.hasMoreTokens();
                    }

                    public String next() {
                        return tokenizer.nextToken();
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }

    public static Iterable<String> tokenize(final StringTokenizer tokenizer) {
        return new Iterable() {
            public Iterator<String> iterator() {
                return new Iterator() {
                    public boolean hasNext() {
                        return tokenizer.hasMoreTokens();
                    }

                    public String next() {
                        return tokenizer.nextToken();
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }

    private StringUtils() {
    }

    private static String __getMachineId() {
        String hostname = null;
        String ethernet = null;
        InetAddress localhost = null;

        try {
            localhost = InetAddress.getLocalHost();
        } catch (UnknownHostException var11) {
            ;
        }

        try {
            int var8;
            if(localhost == null) {
                hostname = "UNKNOWN";
            } else {
                hostname = localhost.getHostName();
                NetworkInterface ex = NetworkInterface.getByInetAddress(localhost);
                if(!ex.isLoopback() && !ex.isVirtual() && !ex.isPointToPoint() && ex.getHardwareAddress() != null) {
                    byte[] nis = ex.getHardwareAddress();
                    StringBuilder ni = new StringBuilder(nis.length << 1);
                    byte[] var9 = nis;
                    var8 = nis.length;

                    for(int aByte = 0; aByte < var8; ++aByte) {
                        byte bytes = var9[aByte];
                        ni.append(Integer.toHexString(255 & bytes));
                    }

                    ethernet = ni.toString();
                }
            }

            if(ethernet == null) {
                StringBuilder var13 = new StringBuilder();
                Enumeration var14 = NetworkInterface.getNetworkInterfaces();

                label69:
                while(true) {
                    NetworkInterface var15;
                    do {
                        do {
                            do {
                                do {
                                    if(!var14.hasMoreElements()) {
                                        if(1 < var13.length()) {
                                            ethernet = var13.deleteCharAt(var13.length() - 1).toString();
                                        }
                                        break label69;
                                    }

                                    var15 = (NetworkInterface)var14.nextElement();
                                } while(var15.isLoopback());
                            } while(var15.isVirtual());
                        } while(var15.isPointToPoint());
                    } while(var15.getHardwareAddress() == null);

                    byte[] var16 = var15.getHardwareAddress();
                    byte[] var10 = var16;
                    int var18 = var16.length;

                    for(var8 = 0; var8 < var18; ++var8) {
                        byte var17 = var10[var8];
                        var13.append(Integer.toHexString(255 & var17));
                    }

                    var13.append("-");
                }
            }

            if(ethernet == null) {
                throw new IllegalStateException("invalid client");
            } else {
                return Base64.encodeToString(("[P]" + hostname + ":" + ethernet).getBytes(Charset.forName("UTF-8")));
            }
        } catch (SocketException var12) {
            throw new IllegalStateException("");
        }
    }

    private static BigInteger __decodeGroups(String encoded) throws IllegalStateException {
        BigInteger orig = BigInteger.ZERO;
        BigInteger max = BigInteger.valueOf(60466176L);

        int j;
        for(int i = encoded.length(); i >= 0; i = j - 1) {
            j = encoded.lastIndexOf(45, i - 1) + 1;
            String str = encoded.substring(j, i);
            int k = __decodeGroup(str);
            orig = orig.multiply(max);
            orig = orig.add(BigInteger.valueOf((long)k));
        }

        return orig;
    }

    private static int __decodeGroup(String grouped) throws IllegalStateException {
        if(grouped.length() != 5) {
            throw new IllegalStateException();
        } else {
            boolean i = true;
            int j = 0;

            for(int k = grouped.length() - 1; k >= 0; --k) {
                char m = grouped.charAt(k);
                int n;
                if(48 <= m && m <= 57) {
                    n = m - 48;
                } else {
                    if(65 > m || m > 90) {
                        throw new IllegalStateException();
                    }

                    n = m - 65 + 10;
                }

                j *= 36;
                j += n;
            }

            return j;
        }
    }

    private static short __calculateCRC32(String mid, int i, byte[] bytes) {
        CRC32 crc32 = new CRC32();
        int j;
        if(mid != null) {
            for(j = 0; j < mid.length(); ++j) {
                crc32.update(mid.charAt(j));
            }
        }

        crc32.update(i);
        crc32.update(i >> 8);
        crc32.update(i >> 16);
        crc32.update(i >> 24);

        for(j = 0; j < bytes.length - 2; ++j) {
            crc32.update(bytes[j]);
        }

        return (short)((int)crc32.getValue());
    }

    private static String __fetchLicense(String machineId) {
        String licenseUrl = "http://gitool.glanway.com/license";
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String license = null;

        try {
            try {
                conn = (HttpURLConnection)(new URL(licenseUrl)).openConnection();
                conn.setRequestProperty("X-MACHINE", machineId);
                int responseCode = conn.getResponseCode();
                if(200 == responseCode) {
                    reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                    license = reader.readLine();
                }
            } finally {
                if(reader != null) {
                    reader.close();
                }

                if(conn != null) {
                    conn.disconnect();
                }

            }
        } catch (IOException var10) {
            ;
        }

        return license;
    }
}
